package com.zx.Service;

import com.zx.Dao.Jdbc;
import com.zx.Moudel.Queue;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * com.zx.Service
 * 26832
 * zx
 * &#064;description:
 * &#064;author: lxm
 * AminServiceImpl
 * &#064;create: 2024-06-22 16:20
 **/

public class AminServiceImpl implements AdminService {
    @Override
    public boolean PassDate(String idcard, String vaccine) throws SQLException {
        String sql = "update queue set state ='已接种' where idcard ='"+idcard+"' and room ='"+vaccine+"'";
        Connection con = null;
        Statement smt = null;
        Jdbc jdbc = new Jdbc();
        int i =-1;
        try{
            con = jdbc.getCon();
            smt = con.createStatement();
            i = smt.executeUpdate(sql);
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            jdbc.Close(con,smt,null);
        }
        return i>0;
    }

    @Override
    public List<Queue> selectQueue() throws SQLException {
        List<Queue> list = new ArrayList<>();
        //获取当前时间格式为yy-mm-dd
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();

        // 创建日期格式化对象
//        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yy-MM-dd");

        // 格式化当前日期
//        String formattedDate = currentDate.format(formatter);
        String sql = "select * from queue where data ='"+currentDate+"' and state ='未接种'";
        Jdbc jdbc = new Jdbc();
        Connection con = null;
        Statement smt = null;
        ResultSet rs = null;
        try{
            con = jdbc.getCon();
            smt = con.createStatement();
            rs = smt.executeQuery(sql);
            while(rs.next()){
                Queue queue = new Queue();
                queue.setId(rs.getInt("id"));
                queue.setIdcard(rs.getString("idcard"));
                queue.setData(rs.getString("data"));
                queue.setRoom(rs.getString("room"));
                list.add(queue);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            jdbc.Close(con,smt,rs);
        }
        return list;
    }
}
